/**

  */ 

/* Includes ------------------------------------------------------------------*/
#include "gd32h759i_eval_tsc2007_ts.h"
#include "gd32h7xx.h"
#include "tsc2007.h"


#ifdef USE_320x240
	#define  A1 	0xFFFFFFAD
	#define  A2 	0xFFFFFFC0
	#define  B1 	0x0004FF9A
	#define  B2 	0x0003D358
#endif
#ifdef USE_480x272
	#define  A1 	0xFFFFFF84
	#define  A2 	0xFFFFFFB5
	#define  B1 	0x00077AB8
	#define  B2 	0x00046EF6
#endif
#ifdef USE_640x480
	#define  A1 	0x000000AC
	#define  A2 	0xFFFFFF7F
	#define  B1 	0xFFFF7918
	#define  B2 	0x00079CBF
#endif
#ifdef USE_800x480	//7inch
	#define  A1 	0xFFFFFF34
	#define  A2 	0xFFFFFF7E
	#define  B1 	0x000C7254
	#define  B2 	0x0007A85A
#endif
//#ifdef USE_800x480	//5inch
//	#define  A1 	0xFFFFFF2C
//	#define  A2 	0xFFFFFF7B
//	#define  B1 	0x000CAB90
//	#define  B2 	0x0007A899
//#endif
#ifdef USE_800x600
	#define  A1 	0xFFFFFF2A
	#define  A2 	0xFFFFFF58
	#define  B1 	0x000CC216
	#define  B2 	0x00097278
#endif
/** @addtogroup BSP
  * @{
  */

/** @addtogroup GD32F4xxI_EVAL
  * @{
  */ 
  
/** @defgroup GD32F4xxI_EVAL_TS
  * @{
  */   

/** @defgroup GD32F4xxI_EVAL_TS_Private_Types_Definitions
  * @{
  */ 
/**
  * @}
  */ 

/** @defgroup GD32F4xxI_EVAL_TS_Private_Defines
  * @{
  */ 
/**
  * @}
  */ 

/** @defgroup GD32F4xxI_EVAL_TS_Private_Macros
  * @{
  */ 
/**
  * @}
  */  

/** @defgroup GD32F4xxI_EVAL_TS_Private_Variables
  * @{
  */ 
//static TS_DrvTypeDef *tsDriver; 
/**
  * @}
  */ 

/** @defgroup GD32F4xxI_EVAL_TS_Private_Function_Prototypes
  * @{
  */ 
/**
  * @}
  */ 

/**
  * @brief  Get the X coordinates by calibration.
  * @param  x: x value from tsc2007
  * @retval The actual X coordinates of the touch screen
  */
uint16_t CALIBRATION_GetX(uint16_t x)
{
  return (((A1 * x) + B1)/1000);
}
/**
  * @brief  Get the Y coordinates by calibration.
  * @param  y: y value from tsc2007
  * @retval The actual y coordinates of the touch screen
  */
uint16_t CALIBRATION_GetY(uint16_t y)
{
  return (((A2 * y) + B2)/1000);
}

/** @defgroup GD32F4xxI_EVAL_TS_Private_Functions
  * @{
  */ 
/*------------------------------------------------------------------------------------------------------------
  Function Name  : tp_int_pin_config
  Description    : Initializes tp int pin.
  Input          : None
  Output         : None
  Return         : None
------------------------------------------------------------------------------------------------------------*/
void tp_int_pin_config(void)
{
	/* enable the int pin GPIO clock */
	rcu_periph_clock_enable(TP_INT_PERIPH);
	/* configure button pin as input */
	gpio_mode_set(TP_INT_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, TP_INT_PIN);
}
/**
  * @brief  Initializes and configures the touch screen functionalities and 
  *         configures all necessary hardware resources (GPIOs, clocks..).
  * @param  xSize: Maximum X size of the TS area on LCD
  *         ySize: Maximum Y size of the TS area on LCD  
  * @retval TS_OK if all initializations are OK. Other value if error.
  */
uint8_t BSP_TS_Init(void) 
{
  uint8_t status = TS_OK;
  tp_int_pin_config();
  tsc2007_Init();
  
  return status;
}

/**
  * @brief  DeInitializes the TouchScreen.
  * @retval TS state
  */
uint8_t BSP_TS_DeInit(void)
{ 
  /* Actually ts_driver does not provide a DeInit function */
  return TS_OK;
}

/**
  * @brief  Configures and enables the touch screen interrupts.
  * @retval TS_OK if all initializations are OK. Other value if error.
  */
uint8_t BSP_TS_ITConfig(void)
{
  return TS_OK;  
}

/**
  * @brief  Gets the touch screen interrupt status.
  * @retval TS_OK if all initializations are OK. Other value if error.
  */
uint8_t BSP_TS_ITGetStatus(void)
{
  /* Return the TS IT status */
  return TS_OK;
}

/**
  * @brief  Returns status and positions of the touch screen.
  * @param  TS_State: Pointer to touch screen current state structure
  * @retval TS_OK if all initializations are OK. Other value if error.
  */
#if defined (USE_320x240) || defined (USE_480x272) || defined (USE_800x480)  || defined (USE_800x600)
uint8_t BSP_TS_GetState(TS_StateTypeDef *TS_State)
{
	TS_State->touchDetected = tsc2007_TS_DetectTouch();
  
  if(TS_State->touchDetected)
  {
//		TS_State->x = tsc2007_MeasureXpos();
//		TS_State->y = tsc2007_MeasureYpos();
		TS_State->touchX[0] = CALIBRATION_GetX(tsc2007_MeasureXpos());
		TS_State->touchY[0] = CALIBRATION_GetY(tsc2007_MeasureYpos());
	}
  return TS_OK;
}
#endif
#if defined (USE_640x480)
uint8_t BSP_TS_GetState(TS_StateTypeDef *TS_State)
{
	TS_State->TouchDetected = tsc2007_TS_DetectTouch();
  
  if(TS_State->TouchDetected)
  {
		//Swap x and y
//		TS_State->x = tsc2007_MeasureYpos();
//		TS_State->y = tsc2007_MeasureXpos();
		TS_State->touchX[0] = CALIBRATION_GetX(tsc2007_MeasureYpos());
		TS_State->touchY[0] = CALIBRATION_GetY(tsc2007_MeasureXpos());
	}
  return TS_OK;
}
#endif
/**
  * @brief  Clears all touch screen interrupts.
  * @retval None
  */
void BSP_TS_ITClear(void)
{
}

/**
  * @}
  */ 

/**
  * @}
  */ 

/**
  * @}
  */ 

/**
  * @}
  */  

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
